import { loginAPI, getUserInfoAPI, getUserAPI } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'
const state = {
  token: getToken() || '',
  userInfo: {}
}
const mutations = {
  settoken(state, val) {
    state.token = val
    setToken(val)
  },
  setUserInfo(state, newUserInfo) {
    state.userInfo = newUserInfo
  },
  leave(state) {
    state.token = ''
    removeToken()
    state.userInfo = {}
  }
}
const actions = {
  async login(store, data) {
    const { data: res } = await loginAPI(data)
    console.log(res)
    store.commit('settoken', res)
    return res
  },
  async getUserInfo(store) {
    const { data: res } = await getUserInfoAPI()
    const { data: res1 } = await getUserAPI(res.userId)
    console.log(res)
    console.log(res1)
    const TotalInfo = {
      ...res,
      ...res1
    }
    store.commit('setUserInfo', TotalInfo)
    return TotalInfo
  }
}
const getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

// export default {
//   namespaced: true,
//   state: {},
//   mutations: {},
//   actions: {},
//   getters: {}
// }
